<?php

include_once($config['webroot']."/includes/seat.class.php");
include_once($config['webroot']."/includes/global.php");
include_once($config['webroot']."/includes/smarty_config.php");

$receiveInfo = $_POST['seatorderdata'];
$res = seat::responseFormat($receiveInfo);
$ticket_info = serialize($res);
$ticket_seat = serialize($res['body']['ticket']);

if(empty($res)){
    msg($config['weburl']."?m=product&s=list",'选座系统异常，请稍后再试');
    return false;
} else {

    if($res['head']['result'] != 200 ){
        msg($config['weburl']."?m=product&s=list",'选座失败，请重新选座');
        return false;
    }
    $session_product = unserialize($_SESSION['product_info']);
    if(empty($session_product)){
        msg($config['weburl']."?m=product&s=list",'你选择的商品不存在，请重新选座');
        return false;
    }
    $db->query("select * from ".SETMEAL." where id = '{$session_product['product_id']}' ");
    $product_info = $db->fetchRow();
    $num = count($res['body']['ticket']) > 0 ? count($res['body']['ticket']) : 1;
    $total = number_format($product_info['price'] * $num,2);

    $db->query("select * from ".MEMBERADDR." where name='{$session_product['ship_name']}' and provinceid='{$session_product['provinceid']}' and cityid='{$session_product['cityid']}' and areaid='{$session_product['areaid']}' and address='{$session_product['ship_addr']}' and mobile='{$session_product['ship_mobile']}' ");
    $is_address_exits = $db->fetchRow();
    if(empty($is_address_exits)){
        //新增地址
        $address_sql = "insert into ".MEMBERADDR." (`userid`,`name`,`provinceid`,`cityid`,`areaid`,`address`,`mobile`) VALUES
 				('$buid','{$session_product['ship_name']}','{$session_product['provinceid']}','{$session_product['cityid']}','{$session_product['areaid']}','{$session_product['ship_addr']}','{$session_product['ship_mobile']}')";
        $db->query($address_sql);
    }

    //选择配送方式
    $shipping = [];
    if($session_product['delivery_way'] == 'express'){
        $ship_sql = "select l.*,c.default_price from ".LGSTEMP." l left join ".LGSTEMPCON." c on l.id = c.temp_id where l.type=0";
        $db->query($ship_sql);
        $shipping = $db->fetchRow();
        $freight = $shipping['default_price'];
    } else {
        $freight = 0;
    }
    $cost = $total;

    //支付方式
    $sql = "select payment_id,payment_name from ".PAYMENT." where payment_type='wechatpay' ";
    $db->query($sql);
    $pay = $db->fetchRow();
    //创建订单
    $order_id=date("Ymdhis").rand(0,9);//订单号
    $pname=!empty($product_info['pname']) ? $product_info['pname'] : $config['company'];//此次购物的产品名总称

    /***生成订单****/
    $sql = "INSERT INTO ".ORDER."(member_id,order_id,create_time,payment_id,payment_name,ship_name,ship_addr,ship_mobile,cost,freight,provinceid,cityid,areaid,shipping_id,shipping_name,point,ticket_info) VALUES
				($buid,'$order_id','".time()."','{$pay['payment_id']}','{$pay['payment_name']}','{$session_product['ship_name']}','{$session_product['ship_addr']}','{$session_product['ship_mobile']}','$cost','$freight','{$session_product['provinceid']}','{$session_product['cityid']}',
				'{$session_product['areaid']}','{$shipping['id']}','{$shipping['title']}','{$product_info['point']}','$ticket_info')";
    $flag=$db->query($sql);

    //--产品信息
    $sql = "INSERT INTO ".ORPRO." (`order_id`,`buyer_id`,`pid`,`pcatid`,`name`,`pic`,`price`,`num`,`time`,`product_id`,`per_date`,`ticket_seat`,`spot_id`) VALUES
			($order_id,$buid,$product_info[pid],'$product_info[catid]','$product_info[pname]','".$product_info['pic']."','".$product_info['price']."','".$num."','".time()."','{$product_info['id']}','{$product_info['performance_start_time']}','$ticket_seat','{$product_info['spot']}')";
    $db->query($sql);
    $pname=$pname.'-'.$product_info['pname'];

    include_once("$config[webroot]/module/product/includes/plugin_order_class.php");
    $order=new order();
    $order->order_log($order_id,'订单创建','创建');
    $order->add_order_delivery($order_id,$shipping,$consignee);
    $order->add_order_payment($order_id,($cost+$freight),$pay);

    include_once("$config[webroot]/module/payment/lang/$config[language].php");
    $post['type']=1;//直接到账
    $post['action']='add';//
    $post['buyer_email']=$buid;
    $post['seller_email']='admin@systerm.com';
    $post['order_id']=$order_id;//外部订单号
    $post['price']=$cost + $freight;;//订单总价，单价元
    $post['return_url']='main.php?m=product&s=admin_orderdetail&id='.$order_id.'&type=buy';//返回地址
    $post['notify_url']='main.php?m=product&s=admin_orderdetail&id='.$order_id.'&type=buy';//异步返回地址。
    $post['note']=$pname;
    pay_get_url($post);//跳转至订单生成页面

    if($re<0)
    {
        if($re==-2)
            msg('main.php?m=payment&s=admin_info','您的支付账户还没有开通');
        if($re==-1)
            msg("$config[weburl]/?m=product&s=cart",'卖家没有开通支付功能，暂不能购买');
    }


}

?>